package cc.minos.servers.openfire.chats
{
	import cc.minos.servers.openfire.managers.OpenFireManager;
	import cc.minos.servers.openfire.ui.textbox.IChatUI;
	import cc.minos.servers.openfire.ui.textbox.IInputUI;
	import flash.events.EventDispatcher;
	import org.igniterealtime.xiff.collections.ArrayCollection;
	import org.igniterealtime.xiff.core.UnescapedJID;
	import org.igniterealtime.xiff.data.im.RosterItemVO;
	import org.igniterealtime.xiff.data.Message;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class SimpleChat extends EventDispatcher
	{
		private var _ui:IChatUI
		private var _inputUI:IInputUI;
		protected var _jid:UnescapedJID;
		protected var _nickname:String;
		
		protected var _presence:String;
		protected var _activate:Boolean;
		
		public function SimpleChat( jid:UnescapedJID )
		{
			_jid = jid;
		}
		
		public function get inputUI():IInputUI
		{
			return _inputUI;
		}
		
		public function set inputUI( value:IInputUI ):void
		{
			_inputUI = value;
		}
		
		public function get ui():IChatUI
		{
			return _ui;
		}
		
		public function set ui( value:IChatUI ):void
		{
			_ui = value;
			setup( _jid );
		}
		
		public function setup( j:UnescapedJID ):void
		{
			var rosterItem:RosterItemVO = RosterItemVO.get( j , true );
			
			//trace("setup> " + _jid );
			//trace("rosterItem> " + rosterItem.jid );
			//_jid = rosterItem.jid;
			
			displayName = rosterItem.nickname;
			presence = rosterItem.show;
		
		/*rosterItem.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, function(evt:PropertyChangeEvent):void {
		   if(evt.property == "show")
		   presence = evt.newValue as String;
		 });*/
		}
		
		public function get occupants():ArrayCollection
		{
			return new ArrayCollection([{ nick: myNickName } , { nick: displayName } ] );
		}
		
		public function get jid():UnescapedJID
		{
			return _jid;
		}
		
		public function set displayName( nickname:String ):void
		{
			_nickname = nickname;
		}
		
		public function get displayName():String
		{
			return _nickname;
		}
		
		public function get myNickName():String
		{
			return OpenFireManager.me.nickname;
		}
		
		public function get presence():String
		{
			return _presence;
		}
		
		public function set presence( value:String ):void
		{
			_presence = value;
		}
		
		public function get activate():Boolean 
		{
			return _activate;
		}
		
		public function set activate(value:Boolean):void 
		{
			_activate = value;
		}
		
		public function insertMessage( message:ChatMessage ):void
		{
			ui.addMessage( message );
		}
		
		public function insertSystemMessage( body:String ):void
		{
			if( OpenFireManager.config.systeminfo )
				ui.addSystemMessage( body );
		}
		
		public function transmitMessage( message:ChatMessage ):void
		{
			OpenFireManager.messageManager.sendMessage( jid , message.body , Message.TYPE_CHAT );
			inputUI.clearText();
		}
		
		public function close():void
		{
		}
	
	}

}